/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ep.m005.ui.views;

import com.vaadin.data.util.BeanItemContainer;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.ComboBox;
import com.vaadin.ui.ComponentContainer;
import com.vaadin.ui.GridLayout;
import com.vaadin.ui.Table;
import com.vaadin.ui.TextField;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Window.Notification;
import ep.libs.implementations.AbstractModule;
import ep.libs.implementations.AbstractView;
import ep.m005.ui.controllers.SelectMedicineController;

/**
 *
 * @author Spyros
 */
public class SelectMedicineView extends AbstractView {

    public static final String SEARCHBYSUB = "Αναζήτηση με δραστική ουσία";
    public static final String SEARCHBYMED = "Αναζήτηση με εμπορική ονομασία";
    private SelectMedicineController controller;
    private Table resultTable;
    private ComboBox comboBox;

    public SelectMedicineView(AbstractModule module) {
        super(module);
    }

    public SelectMedicineView(AbstractModule module, Object[] args, ComboBox comboBox) {
        super(module, args);
        this.comboBox = comboBox;
    }

    @Override
    public ComponentContainer getView() {
        viewLayout = new VerticalLayout();
        controller = new SelectMedicineController();
        initializeComponents();
        return viewLayout;
    }

    private void initializeComponents() {
        //sub field
        final TextField subField = new TextField();
        //sub button
        Button subButton = new Button(SEARCHBYSUB);
        //med field
        final TextField medField = new TextField();
        //med button
        Button medButton = new Button(SEARCHBYMED);
        //non visible table with meds and generated subs
        resultTable = new Table();
        resultTable.setWidth("750px");
        resultTable.setPageLength(5);
        resultTable.setVisible(false);
        resultTable.setSelectable(true);
        //ok button
        Button okButton = new Button("Επιλογή");

        GridLayout gridLayout = new GridLayout(2, 3);
        gridLayout.setSpacing(true);
        gridLayout.addComponent(subField);
        gridLayout.addComponent(medField);
        gridLayout.addComponent(subButton);
        gridLayout.addComponent(medButton);
        gridLayout.addComponent(resultTable, 0, 2, 1, 2);

        viewLayout.addComponent(gridLayout);
        viewLayout.addComponent(okButton);

        //listeners
        subButton.addListener(new Button.ClickListener() {

            @Override
            public void buttonClick(ClickEvent event) {
                refreshResultTable(controller.getMedicineBySubstance(subField.getValue().toString()));
                resultTable.setVisible(true);
            }
        });

        medButton.addListener(new Button.ClickListener() {

            @Override
            public void buttonClick(ClickEvent event) {
                refreshResultTable(controller.getMedicineByMedName(medField.getValue().toString()));
                resultTable.setVisible(true);
            }
        });

        okButton.addListener(new Button.ClickListener() {

            @Override
            public void buttonClick(ClickEvent event) {
                if (resultTable.getValue() != null) {
                    comboBox.setValue(resultTable.getValue());
                    event.getButton().getWindow().getParent().removeWindow(event.getButton().getWindow());
                }else{
                    event.getButton().getWindow().showNotification("Επιλέξτε ένα φάρμακο απο τον πίνακα", Notification.TYPE_ERROR_MESSAGE);
                }
            }
        });

    }

    private void refreshResultTable(BeanItemContainer beanItemContainer) {
        resultTable.setContainerDataSource(beanItemContainer);
        resultTable.setVisibleColumns(new String[]{"name", "t010Substances", "price"});
        resultTable.setColumnHeaders(new String[]{"Εμπορική ονομασία", "Δραστικές ουσίες", "Τιμή"});
    }
}
